import axios from 'axios'
import qs from 'qs'
import { Indicator, MessageBox } from 'mint-ui';
let needLoadingRequestCount = 0
export function showFullScreenLoading () {
  if (needLoadingRequestCount === 0) {
    startLoading()
  }
  needLoadingRequestCount++
}
export function tryHideFullScreenLoading () {
  if (needLoadingRequestCount <= 0) return
  needLoadingRequestCount--
  if (needLoadingRequestCount === 0) {
    endLoading()
  }
}

let loading
function startLoading () {
  Indicator.open({
		text: '加载中...',
		spinnerType: 'fading-circle'
	})
}
function endLoading () {
  Indicator.close()
}

axios.interceptors.request.use(function (config) {
  showFullScreenLoading()
  return config
}, error => {
  return Promise.resolve(error)
})

axios.interceptors.response.use((response) => {
  tryHideFullScreenLoading()
  return response
}, error => {
  return Promise.resolve(error)
})

function checkStatus (response) {
  if (response && ((response.status === 200 || response.status === 304 || response.status === 400))) {
    return response.data
  }
  return {
    code: '404',
    message: '网络异常'
  }
}
let gp = '/gp'
export default {
  gp,
  messageTwo (message, title) {
		MessageBox.alert(message).then(action => {
		})
  },
  alert (message, func) {
		MessageBox.alert(message).then(action => {
			func()
		})
  },
  requestget (url, data) {
    return axios.get( gp + url, {
      params: data,
    }).then((res) => {
      return checkStatus(res)
      if (res.data.code === -1) {
        MessageBox.alert(res.data.data, '提示信息', {
          confirmButtonText: '确定'
        })
        return false
      }
    }).catch(function () {
      tryHideFullScreenLoading()
      MessageBox.alert('系统错误，联系管理员', '提示信息', {
        confirmButtonText: '确定',
        callback: action => {
        }
      })
    })
  },
	requestPost (url, data) {
		return axios({
			method: 'post',
			baseURL: process.env.BASE_URL,
			url: gp + url,
			data: qs.stringify(data),
			headers: {
				'X-Requested-With': 'XMLHttpRequest',
				'Content-Type': 'multipart/form-data;'
			},
			timeout: 10000
		}).then((res) => {
			return checkStatus(res)
			if (res.data.code === -1) {
				MessageBox.alert(res.data.data, '提示信息', {
					confirmButtonText: '确定'
				})
				return false
			}
		}).catch(function () {
			tryHideFullScreenLoading()
			MessageBox.alert('系统错误，联系管理员', '提示信息', {
				confirmButtonText: '确定',
				callback: action => {
				}
			})
		})
	},
  request (method, url, data) {
    return axios({
      method: method,
      baseURL: process.env.BASE_URL,
      url: gp + url,
      data: qs.stringify(data),
      headers: {
        'X-Requested-With': 'XMLHttpRequest',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
      },
      timeout: 10000
    }).then((res) => {
      return checkStatus(res)
      if (res.data.code === -1) {
        MessageBox.alert(res.data.data, '提示信息', {
          confirmButtonText: '确定'
        })
        return false
      }
    }).catch(function () {
      tryHideFullScreenLoading()
      MessageBox.alert('系统错误，联系管理员', '提示信息', {
        confirmButtonText: '确定',
        callback: action => {
        }
      })
    })
  },
  request2 (method, url, data) {
    return axios({
      method: method,
      baseURL: process.env.BASE_URL,
      url: gp + url,
      data: data,
      headers: {
        'X-Requested-With': 'XMLHttpRequest',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
      },
      timeout: 10000
    }).then((res) => {
      return checkStatus(res)
      if (res.data.code === -1) {
        MessageBox.alert(res.data.data, '提示信息', {
          confirmButtonText: '确定'
        })
        return false
      }
    }).catch(function () {
      tryHideFullScreenLoading()
      MessageBox.alert('系统错误，联系管理员', '提示信息', {
        confirmButtonText: '确定',
        callback: action => {
        }
      })
    })
  }
}
